/**
 * 路由demo文件
 *
 * @author bian
 * @createDate 2016.10.1
 */
let render = require("../instances/render")
const {Check} = require("../lib/check")
const auth = require("../instances/auth")

// 管理员身份验证中间件
function * adminValidate(next){
    let ctx = this
    let adminer = yield auth.admin(ctx)
    if(! adminer){
        ctx.redirect("/admin_login")
        return
    }
    yield next
}

module.exports = (router) => {

    router.get("/", function *() {
        let ctx = this
        let task = ctx.task

        // get carousels
        let carousels = yield task.getCarousel()

        // get courses
        let courses = yield task.getIndexCourses()

        // get user
        let user = yield task.getUserByCookie()

        // success
        ctx.body = yield render("user_index.html",{
            user : JSON.stringify(user),
            carousels : JSON.stringify(carousels),
            courses : JSON.stringify(courses)
        })
    })

    // 主页
    router.get("/courses", function *() {
        let ctx = this
        let task = ctx.task

        // get courses
        let courses = yield task.getCourses()

        // get user
        let user = yield task.getUserByCookie()

        ctx.body = yield render("user_home.html",{
            courses : JSON.stringify(courses),
            user : JSON.stringify(user)
        })
    })

    // 视频播放
    router.get("/user_video/:courseId", function* () {
        let ctx = this
        let task = ctx.task

        // get course
        let course = yield task.getCourse()

        // get comments
        let comments = yield task.getComments()

        // get notes
        let notes = yield task.getNotes()

        // get files
        let files = yield task.getFiles()

        // get courses
        let courses = yield task.getIndexCourses()

        // get user
        let user = yield task.getUserByCookie()

        // record history
        if(user !== "NULL"){
            yield task.recordHistory(ctx.params.courseId,user.id)
        }

        ctx.body = yield render("user_video.html",{
            course : JSON.stringify(course),
            videoTitle : course.title,
            user : JSON.stringify(user),
            comments : JSON.stringify(comments),
            notes : JSON.stringify(notes),
            files : JSON.stringify(files),
            courses : JSON.stringify(courses)
        })
    })

    router.get('/personal_center', function* () {
        let ctx = this
        let task = ctx.task

        // get user
        let user = yield task.getUserByCookie()
        if(typeof user === "undefined" || user === "NULL"){
            ctx.redirect("/")
            return
        }

        // get histories
        let histories = yield task.getHistories(user.id)

        // get collect courses
        let collectCourses = yield task.getCollectCourses(user.id)

        // get comments
        let comments = yield task.getUserCommentsOrNotes(user.id,0)

        // get notes
        let notes = yield task.getUserCommentsOrNotes(user.id,1)

        ctx.body = yield render('personal_center.html',{
            user : JSON.stringify(user),
            histories : JSON.stringify(histories),
            collectCourses : JSON.stringify(collectCourses),
            comments : JSON.stringify(comments),
            notes : JSON.stringify(notes)
        })
    })

    router.get("/admin_login", function *(){
        let ctx = this
        ctx.body = yield render("admin_login.html")
    })

    router.get("/admin_home", adminValidate ,function *(){
        let ctx = this
        ctx.body = yield render("admin_home.html")
    })

    router.get("/admin_home/course",adminValidate,function *(){
        let ctx = this
        ctx.body = yield render("admin_home_course.html",{
            menuIdx : 1
        })
    })

    router.get("/admin/overview",adminValidate,function *(){
        let ctx = this
        ctx.body = yield render("admin_overview.html",{
            menuIdx : 0
        })
    })

    router.get("/admin/course_edit",adminValidate,function *(){
        Check(this,["courseId","toInt"])

        let ctx = this
        ctx.body = yield render("admin_course_edit.html",{
            menuIdx : 1
        })
    })

    router.get("/admin/students",adminValidate,function *(){
        // Check(this,["courseId","toInt"])

        let ctx = this
        ctx.body = yield render("admin_students.html",{
            menuIdx : 2
        })
    })

    router.get("/admin/forum",adminValidate,function *(){
        let ctx = this
        ctx.body = yield render("admin_home_forum.html",{
            menuIdx : 3
        })
    })

    router.get("/admin/articles",adminValidate,function *(){
        let ctx = this
    })

    router.get("/admin/index_pictures",adminValidate,function *(){
        let ctx = this
        ctx.body = yield render("admin_home_pictures.html",{
            menuIdx : 5
        })
    })

    // 路由重定向
    router.get("/admin/course_edit/*",adminValidate,function *(){
        let ctx = this
        ctx.redirect("/admin_home/course")
    })

    // 重定向
    router.get("/admin/*",function *(){
        let ctx = this
        ctx.redirect("/admin/overview")
    })
}